package com.want.evenchat.controller.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.text.TextUtilsCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.want.evenchat.R;
import com.want.evenchat.model.Model;
import com.want.evenchat.model.bean.UserInfo;

/**
 * Created by Want on 2018/5/3.
 */

public class LoginActivity extends Activity{

    EditText edittext_login_username,edittext_login_password;

    Button button_login_login;

    TextView textview_login_register;

    private String edit_username_login;
    private String edit_password_login;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        //初始化控件
        init();

        //初始化监听
        initListener();
    }

    private void init(){
        edittext_login_username = findViewById(R.id.edittext_login_username);
        edittext_login_password = findViewById(R.id.edittext_login_password);

        button_login_login = findViewById(R.id.button_login_login);

        textview_login_register = findViewById(R.id.textview_login_register);
    }

    private void initListener(){

        button_login_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用登录的方法
                login();
            }
        });

        textview_login_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用注册的方法
                register();
            }
        });
    }

    //登录按钮业务逻辑
    private void login(){
        //1.获取用户输入的用户名和密码
        edit_username_login = edittext_login_username.getText().toString().trim();
        edit_password_login = edittext_login_password.getText().toString().trim();

        //2.判断获取的用户名和密码
        //(1.判断用户名和密码是否为空)
//        if (edit_username_login == null && edit_username_login.equals("") && edit_password_login == null && edit_password_login.equals("")){
//            return;
//        }
        if (edit_username_login == null  && edit_username_login.equals("")){
            Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_password_login == null  && edit_password_login.equals("")){
            Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_username_login.contains(" ")){
            Toast.makeText(LoginActivity.this, "用户名含有非法字符", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_password_login.contains(" ")){
            Toast.makeText(LoginActivity.this, "密码含有非法字符", Toast.LENGTH_SHORT).show();
            return;
        }

        //3.登录逻辑处理
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //环信服务器登录
                EMClient.getInstance().login(edit_username_login, edit_password_login, new EMCallBack() {
                    //登录成功后的处理
                    @Override
                    public void onSuccess() {
                        //对模型层数据的处理
                        Model.getInstance().loginSuccess(new UserInfo(edit_username_login));

                        //保存用户帐号信息到本地数据库
                        Model.getInstance().getUserAccountDao().addAccount(new UserInfo(edit_username_login));

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //提示登录成功
                                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();

                                //跳转到主页面
                                startActivity(new Intent(LoginActivity.this , MainActivity.class));
                                finish();
                            }
                        });
                    }

                    //登录失败的处理
                    @Override
                    public void onError(int i, final String s) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //提示登录失败
                                Toast.makeText(LoginActivity.this, s , Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    //登录中的处理
                    @Override
                    public void onProgress(int i, String s) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //提示登录成功
                                Toast.makeText(LoginActivity.this, "正在登录", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
    }

    //注册text业务逻辑处理
    private void register(){
        //1.获取用户输入的用户名和密码
        edit_username_login = edittext_login_username.getText().toString();
        edit_password_login = edittext_login_password.getText().toString();

        //2.判断获取的用户名和密码
        //(1.判断用户名和密码是否为空)
        if (edit_username_login == null  && edit_username_login.equals("")){
            Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_password_login == null  && edit_password_login.equals("")){
            Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_username_login.contains(" ")){
            Toast.makeText(LoginActivity.this, "用户名含有非法字符", Toast.LENGTH_SHORT).show();
            return;
        }else if (edit_password_login.contains(" ")){
            Toast.makeText(LoginActivity.this, "密码含有非法字符", Toast.LENGTH_SHORT).show();
            return;
        }

        //(2.判断用户名长度不能小于3位)
        if (edit_username_login.length() < 3){
            Toast.makeText(this, "用户名须大于两个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        //3.去服务器注册帐号
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //去环信服务器注册帐号
                try {
                    //两个参数代表要注册的用户名和密码
                    EMClient.getInstance().createAccount(edit_username_login,edit_password_login);

                    //注册成功后提示用户(更新页面显示)
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this, "该用户已存在", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }


}
